﻿/*
 * Created by SharpDevelop.
 * User: zbyn
 * Date: 2010-10-23
 * Time: 09:34
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Facebook.Schema;
using FacebookPlugin.Properties;
using Facebook.Winforms;
using System.Collections.Generic;

namespace FacebookPlugin
{
	/// <summary>
	/// Description of ctlFacebook.
	/// </summary>
	public partial class ctlFacebook : UserControl
	{
		public ctlFacebook()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			facebookService1.ApplicationKey = Settings.Default.api_key;
            facebookService1.ConnectToFacebook(new List<Enums.ExtendedPermissions>() { Enums.ExtendedPermissions.read_stream, Enums.ExtendedPermissions.publish_stream});
		}
		
		void CtlFacebook_Load(object sender, EventArgs e)
		{
			ListenToEvents(true);
			try
			{
				var friends = facebookService1.Friends.GetUserObjects();
				var me = facebookService1.Users.GetInfo();
				LoadUserBasedControls(me);
				friendList1.Friends = friends;
                //facebookService1.
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			//	Close();
			}

            //facebookService1.
		}
		
		private void ListenToEvents(bool listen)
		{
			if (listen)
			{
				friendList1.FriendSelected += FriendList1_FriendSelected;
			}
		}
		
		
		void FriendList1_FriendSelected(object sender, Facebook.Winforms.FriendSelectedEventArgs e)
		{
			LoadUserBasedControls(e.User);
		}
		
		private void LoadUserBasedControls(user user)
		{
			
			profile2.User = user;
		    photoAlbum2.Albums = facebookService1.Photos.GetAlbums(user.uid.Value);
		}

        private void button1_Click(object sender, EventArgs e)
        {
            facebookService1.LogOff();
        }
		
	}
}
